/*
 * Created by JFormDesigner on Mon Jun 26 11:26:20 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.BookDao;
import org.scct.chenhui.model.BookBean;
import org.scct.chenhui.utils.ActionDo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Administrator
 */
public class PnlBookManager extends JPanel {

    private DefaultTableModel model;

    public PnlBookManager() {
        initComponents();
        initData();
    }

    private void initData() {
//        初始化-添加表头
        model = new DefaultTableModel();
        String []cols={"编号","书名","作者","类别","出版社","定价","数量","状态","备注"};
        for (String col : cols) {
            model.addColumn(col);
        }
        table1.setModel(model);
        getTableData();

        btnAdd.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DlgBookAdd dlgBookAdd = new DlgBookAdd(null, new ActionDo() {
                    @Override
                    public void afterdo(Object obj) {
                        getTableData();
                    }
                });
                dlgBookAdd.setModal(true);
                dlgBookAdd.setVisible(true);
            }
        });

        btnUpdate.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(table1.getSelectedRow()<0)
                {
                    JOptionPane.showMessageDialog(null,"请选择需要修改的图书");
                    return;
                }
                int id = Integer.parseInt(table1.getValueAt(table1.getSelectedRow(), 0).toString());
                DlgBookUpdate dlgBookUpdate = new DlgBookUpdate(null, id, new ActionDo() {
                    @Override
                    public void afterdo(Object obj) {
                        getTableData();
                    }
                });
                dlgBookUpdate.setModal(true);
                dlgBookUpdate.setVisible(true);
            }
        });


    }
    private void getTableData() {
        model.setRowCount(0);
        BookDao bookDao = new BookDao();
        List<BookBean> books = bookDao.getAllBooks();
        for (BookBean book : books) {
          Object [] objs={book.getId(),book.getBookName(),book.getAuthor(),book.getTypeName(),
          book.getPublishName(),String.format("￥%.2f",book.getPrice()),book.getNumber(),
          book.getStatus()==0?"下架":"上架",book.getRemark()};
          model.addRow(objs);
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax.
        swing. border. EmptyBorder( 0, 0, 0, 0) , "JF\u006frmDesi\u0067ner Ev\u0061luatio\u006e", javax. swing. border
        . TitledBorder. CENTER, javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("Dialo\u0067"
        ,java .awt .Font .BOLD ,12 ), java. awt. Color. red) , getBorder
        ( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java
        .beans .PropertyChangeEvent e) {if ("borde\u0072" .equals (e .getPropertyName () )) throw new RuntimeException
        ( ); }} );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e\u56fe\u4e66");
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u56fe\u4e66");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
